package com.iwop.moto.controller;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author huyuangui on 2016/7/18.
 */
@Controller
public class CityController extends  BaseController {

    @Autowired
    private CityService cityService;




    /**
     * liuwb 2016-07-19 17:10
     * 功能:省份id获取省份信息
     * 参数param: id
     */
    @RequestMapping("/api/province/byid")
    @ResponseBody
    public ResultModel findProvinceById(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel =  cityService.findProvinceById(SRModel);
            if(SRModel==null){
               return getErr(ErrCode.ERROR_NOCOUNT);
            } else {
               return getSucceed(SRModel);
            }
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * liuwb 2016-07-19 17:17
     * 功能:获取全部省份信息
     */
    @RequestMapping("/api/all/province")
    @ResponseBody
    public ResultModel findAllProvince(){
        try {
            List<SuperRecordModel> list =  cityService.findAllProvince();
            if(list.size()==0){
                return getErr(ErrCode.ERROR_NOCOUNT);
            } else {
                return getSucceed(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }


    /**
     * liuwb 2016-07-19 17:23
     * 功能:根据省份id获取【市】列表信息
     * 参数param: provinceId
     */
    @RequestMapping("/api/ctiy/father")
    @ResponseBody
    public ResultModel findCityListByFather(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            List<SuperRecordModel> list =  cityService.findCityListByFather(SRModel);
            if(list.size() == 0){
                return getErr(ErrCode.ERROR_NOCOUNT);
            } else {
                return getSucceed(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * liuwb 2016-07-19 17:24
     * 功能:根据市id获取【区】列表信息
     * 参数param: cityId
     */
    @RequestMapping("/api/area/father")
    @ResponseBody
    public ResultModel findAreaListByFather(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            List<SuperRecordModel> list =  cityService.findAreaListByFather(SRModel);
            if(list.size() == 0){
                return getErr(ErrCode.ERROR_NOCOUNT);
            } else {
                return getSucceed(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }
}
